//
//  CommentController.m
//  GuShiNews
//
//  Created by 丁一 on 2019/8/16.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "CommentController.h"
#import "GetMyCommentViewModel.h"
#import "CommentModel.h"
#import "NewsCommentCell.h"
#import "NewsModel.h"
#import "NewsAction.h"
#import "HiJonesClass.h"

@interface CommentController ()<GetMyCommentDelegate>{
    NSInteger page;
}

@end

@implementation CommentController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([_type isEqualToString:@"0"]) {
        self.navigationItem.title=@"收到的评论";
    }else{
        self.navigationItem.title=@"发出的评论";
    }
    
    self.Data_array = [NSMutableArray new];
    
    [self.view addSubview:self.TableView];
    MJHiCarHeader *header           = [MJHiCarHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadnew)];
    header.automaticallyChangeAlpha    = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden           = YES;
    self.TableView.mj_header          = header;
    self.TableView.mj_header.automaticallyChangeAlpha = YES;
    [self.TableView.mj_header beginRefreshing];
    self.TableView.mj_footer           = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadmore];
    }];
    self.TableView.mj_footer.hidden    = YES;
}

- (void)loadnew{
    page = 0;
    NSMutableDictionary *postdic = [NSMutableDictionary new];
    [postdic setValue:[UserModel info].userid forKey:@"userid"];
    [postdic setValue:_type forKey:@"type"];
    [postdic setValue:[NSString stringWithFormat:@"%li",(long)page] forKey:@"page"];
    GetMyCommentViewModel *getcomment = [[GetMyCommentViewModel alloc] init:postdic];
    getcomment.delegate=self;
}

- (void)loadmore{
    
}

- (void)GetMyCommentSucces:(int)value array:(NSMutableArray *)array page:(NSInteger)page{
    [self endloading];
    if (value==1) {
        [[NSNotificationCenter defaultCenter] postNotificationName:ROOTGOTOLOGIN object:@{@"done":@"0"}];
        if (page==0) {
            [self.Data_array removeAllObjects];
            page = 0;
        }
        if (array.count<20) {
            self.TableView.mj_footer.hidden = YES;
        }
        [self.Data_array addObjectsFromArray:array];
        [self.TableView reloadData];
    }else{
        
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.Data_array.count;
}

#pragma mark - UITableView DataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommentModel *model = [self.Data_array objectAtIndex:indexPath.row];
    NewsCommentCell *cell = [NewsCommentCell cellForTableView:tableView cellForRowAtIndexPath:indexPath];
    cell.Model = model;
    cell.SuperVC = self;
    cell.Morebutton.hidden = YES;
    cell.seleckblock = ^(NSInteger type) {
        if (type==0) {

        }else{
           
        }
    };
    return cell;
}

#pragma mark - UITableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommentModel *model = [self.Data_array objectAtIndex:indexPath.row];
    if ([model.to_comment_id intValue]>0) {
        return 85 + model.comment_height + model.to_comment_height;
    }else{
        return 85 + model.comment_height;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CommentModel *model = [self.Data_array objectAtIndex:indexPath.row];
    
    NewsModel *newsmodel = [NewsModel new];
    newsmodel.news_id = model.news_id;
    newsmodel.news_tittle = model.news_tittle;
    newsmodel.news_info = model.news_info;
    newsmodel.news_write = model.news_write;
    newsmodel.news_come = model.news_come;
    newsmodel.news_type = model.news_type;
    newsmodel.news_img = model.news_img;
    newsmodel.news_count = model.news_count;
    newsmodel.news_date = model.news_date;
    
    newsmodel.title_height = [HuaHereClass size:model.news_tittle font:[UIFont boldSystemFontOfSize:23] width:RECRWITCH-50 height:1000].height+20;
    if ([newsmodel.news_img isEqualToString:@""]) {
        newsmodel.news_show_type = 0;
        newsmodel.news_height = 105;
    }else{
        newsmodel.news_show_type = 1;
        newsmodel.news_height = 120;
    }

    NSString *resultStr = [newsmodel.news_info stringByReplacingOccurrencesOfString:@"/n" withString:@"\n"];
    newsmodel.news_info = resultStr;
    newsmodel.info_height =[HiJonesClass getContent:newsmodel.news_info width:RECRWITCH-50 font:[UIFont systemFontOfSize:17] jianju:3]+30;
//    newsmodel.info_height = [HuaHereClass size:newsmodel.news_info font:[UIFont systemFontOfSize:18] width:RECRWITCH-50 height:100000].height+35;
    
    [NewsAction Actiontodone:JonesGOTO_NewsInfo view:self dictionary:@{@"model":newsmodel}];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
